import * as api from '@/api/formApi'
import axios from 'axios'
import cookie from 'js-cookie'

import request from '@/utils/request'

const eurekaName = '/api/ziguang-doc'

export function addFile(data) {
  return request({
    url: `${eurekaName}/api/v1/docServer/file/withTagId`,
    method: 'POST',
    data: data.formData,
    headers: {
      workspaceId: data.factoryId,
      // 'Content-Type': undefined
    },
    timeout: 30000
  })
}


export const getDictionaryTypeList = () => {
  let params = {
    page: 1,
    size: 9999,
    factoryId: window.localStorage.factoryId
  }
  return api.getDataDicList(params).then(data => {
    if (data.data && data.data.rows) {
      return data.data.rows.map(one => ({
        id: one.dataDicId,
        name: one.dataDicName,
      }))
    } else {
      return []
    }
  })
}

export const getDictionaryList = (dataDicId) => {
  let params = {
    dataDicId
  }
  return api.getDataDicItem(params).then(data => {
    if (data.data) {
      data.data.map(item => {
        item.label = item.dicTagName
        item.value = item.tagValue
      })
      return data.data
    } else {
      return []
    }
  })
}

export const getRuleList = () => {
  let params = {
    page: 1,
    size: 9999,
  }
  let data = {
    factoryId: window.localStorage.factoryId
  }
  return api.getRuleList(params, data).then(data => {
    if (data.data) {
      return data.data.rows.map(one => ({
        id: one.dataRuleId,
        name: one.dataRuleName,
      }))
    } else {
      return []
    }
  })
}

export const getApiList = () => {
  let params = {
    page: 1,
    size: 9999,
  }
  let data = {
    factoryId: window.localStorage.factoryId
  }
  return api.getApiList(params, data).then(data => {
    if (data.data) {
      return data.data.rows.map(one => ({
        id: one.dataInterfaceId,
        name: one.interfaceName,
      }))
    } else {
      return []
    }
  })
}

export const invokeInterface = (id, params, body) => {
  if (!id) {
    return Promise.resolve()
  }
  params.factoryId = window.localStorage.factoryId
  return api.invokeInterface(id, params).then(res => {
    if (res.data) {
      return JSON.parse(res.data)
    }
    return null
  })
}


export const getValidateRule = (id) => {
  let params = {
    page: 1,
    size: 9999
  }
  let data = {
    factoryId: window.localStorage.factoryId,
    dataRuleId: id
  }
  return api.getRuleList(params, data).then(res => {
    if (res.data) {
      return res.data.rows
    }
    return []
  })
}

export const setFormTypeItem = (form) => {
  form.factoryId = window.localStorage.factoryId
  return api.setFormTypeItem(form)
}

export const getFormTypeItem = () => {
  let data = { factoryId: window.localStorage.factoryId }
  let params = {
    page: 1,
    size: 9999
  }
  return api.getFormTypeItem(params, data)
}








export const getDataByUrl = (url, method, params, body) => {
  if (!params) {
    params = {}
  }
  return axios({
    method,
    url,
    data: body,
    params,
    headers: {
      'X-token': cookie.get('Admin-Token')
    }
  }).then(config => config.data)
}

// 获得所有数据接口
export const getInterfaceListAll = () => {
  // return api.getInterfaceListAll().then(data => {
  //   if (data) {
  //     return data.map(one => ({
  //       id: one.id,
  //       name: one.name
  //     }))
  //   } else {
  //     return []
  //   }
  // })
}
// 获得所有的数据字典
export const getDataDictionaryAll = () => {
  // return api.getDictionaryListAll().then(data => {
  //   if (data) {
  //     return data.map(one => ({
  //       id: one.id,
  //       name: one.name
  //     }))
  //   } else {
  //     return []
  //   }
  // })
}

// 获得应用的页面
export const getPageList = () => {
  // return api.getPageList().then(data => {
  //   return data.rows.map(one => ({ id: one.id, name: one.name }))
  // })
}

// 获得校验规则
export const getValidateRuleList = () => {
  return []
  // return api.getValidateRuleList(1, 999999).then(data => {
  //   return data.rows.map(one => ({ id: one.id, name: one.name, value: one.value }))
  // })
}

export const getTemplateList = () => {
  // return api.getTemplateList().then(data => {
  //   return data.map(one => ({ id: one.id, name: one.templateName }))
  // })
}
export const getTemplate = (id) => {
  // if (!templateCache[id]) {
  //   templateCache[id] = api.getTemplate(id)
  // }
  // return templateCache[id]
}
